<?php 
	if (!isset($_POST['toWho']) || !isset($_POST['title']) || !isset($_POST['myContent'])) {
		exit();
	}

	require_once ('../conf/pdoConnect.php');
	require_once ('./check.php');
	require_once "./email.class.php";

//******************** 配置信息 ********************************
	$smtpserver = "smtp.163.com";			//SMTP 服务器
	$smtpserverport = 25;					//SMTP·端口
	$smtpusermail = "";	//SMTP·发件箱
	$smtpemailto = $_POST['toWho'];		//收件箱
	$smtpuser = "";				//SMTP·用户名
	$smtppass = "";				//SMTP 密码
	$mailtitle = $_POST['title'];			//主题
	$mailcontent = $_POST['myContent'];		//内容
	$mailtype = "HTML";						//格式
//************************ 配置信息 ****************************
	$smtp = new smtp($smtpserver,$smtpserverport,true,$smtpuser,$smtppass);//true表示身份验证
	$smtp->debug = false;//是否显示发送调试信息
	$state = $smtp->sendmail($smtpemailto, $smtpusermail, $mailtitle, $mailcontent, $mailtype);

	echo "<div style='width:300px; margin:36px auto;'>";
	if($state==""){
		echo "邮件发送失败,请检查是否填写有误";
		echo "<a href='../writeMail.php'>点击返回</a>";

		$db = null;
		exit();
	} else {
		$belongId = $_SESSION['uid'];

		$sendTime = date('Y-m-d h:i:s');

		$toWho = $_POST['toWho'];
		$mailTitle = $_POST['title'];
		$mailContent = $_POST['myContent'];

		$toWho = $toWho;
		$mailTitle = $mailTitle;
		$mailContent = $mailContent;

		sendMail($db, $mailTitle, $mailContent, $sendTime, $belongId, $toWho);
		$db = null;

		echo "邮件发送成功";
		echo "<a href='./getMailList.php'>点击返回</a>";
		echo "</div>";		
	}
	
	function sendMail($db, $mailTitle, $mailContent, $sendTime, $belongId, $toWho)
	{
		$sql = "insert into sendMail(mailTitle, mailContent, sendTime, belongId, toWho) values (?, ?, ?, $belongId, ?)";
		$stmt = $db->prepare($sql);
		$exec = $stmt->execute(array($mailTitle, $mailContent, $sendTime, $toWho));
		if ($exec) {
			return true;
		}
	}